package cn.itsource.controller;

import cn.itsource.bean.User;
import cn.itsource.rate.RateLimit;
import cn.itsource.service.IUserService;
import cn.itsource.util.JsonResult;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-16 09:47:00
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 验证手机号
     * @param phone
     * @return
     */
    @GetMapping("/checkphone/{phone}")
    public JsonResult checkPhone(@PathVariable("phone") String phone){
        //1.验证手机号的合法性
       return userService.checkPhone(phone);
    }

    /**
     * 发送验证码
     * @RateLimit : number 在一定秒数中可触发次数，cycle ：设定的秒数，在此时间内肉再次发送请求，则时间重置
     * @param phone
     * @return
     */
    @RateLimit(number = 1, cycle = 60)
    @GetMapping("/sendmobilecode/{phone}")
    public JsonResult sendMobileCode(@PathVariable("phone") String phone){
        return userService.sendMobileCode(phone);
    }

    /**
     *  用户手机注册最后的的验证
     * @param user
     * @return
     */

    @PostMapping
    public JsonResult registered(@RequestBody  @Validated(User.Phone.class) User user){
//        log.info("用户信息，{}",user.toString());
        return userService.registered(user);
    }


    public static void main(String[] args) {
        String str = "{\"title\":\"json在线解析（简版） -JSON在线解析\",\"json.url\":\"https://www.sojson.com/simple_json.html\",\"keywords\":\"json在线解析\",\"功能\":[\"JSON美化\",\"JSON数据类型显示\",\"JSON数组显示角标\",\"高亮显示\",\"错误提示\",{\"备注\":[\"www.sojson.com\",\"json.la\"]}],\"加入我们\":{\"qq群\":\"259217951\"}}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        Map<String, Object> mapJson =new HashMap<>();
        Iterator it =jsonObject.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            mapJson.put(entry.getKey(), entry.getValue());
        }
        System.out.println(mapJson);
        String keywords = mapJson.get("keywords").toString();
        System.out.println(keywords);
        String 备注 = jsonObject.get("备注").toString();
        System.out.println(备注);

    }
}
